package com.qfjy.exception;

import com.alibaba.csp.sentinel.slots.block.degrade.DegradeException;
import com.alibaba.csp.sentinel.slots.block.flow.FlowException;
import com.qfjy.util.resulst.ResultCode;
import com.qfjy.util.resulst.ResultJson;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice  //@ControllerAdvice  @ResponseBody
public class CustomExceptionHandler {

    /**
     * #TODO 流控规则 全局异常处理
     * @return
     */
    @ExceptionHandler  //默认会拦截方法入参的 异常类型 FlowException
    public Object  exceptionFlowHandler(FlowException e){
        return new ResultJson("触发流控-限流，稍候再试", ResultCode.SYS_ERROR);
    }

    /**
     * #TODO 熔断（降级规则 全局异常处理
     * @return
     */
    @ExceptionHandler  //默认会拦截方法入参的 异常类型 DegradeException
    public Object  exceptionDegradeHandler(DegradeException e){
        return new ResultJson("触发熔断降级-，，系统繁忙，稍候再试", ResultCode.TOO_FREQUENT);
    }


   // @ExceptionHandler  //默认会拦截方法入参的 异常类型 RuntimeException
    //public Object  exceptionHandler(RuntimeException e){
    //    return new ResultJson(e.getMessage(), ResultCode.FAIL);
   // }
}
